﻿Imports System.Data.OleDb
Module XL_LoaiThu
    Structure LoaiThu
        Private _iD As Integer
        Property ID() As Integer
            Get
                Return _iD
            End Get
            Set(ByVal value As Integer)
                _iD = value
            End Set
        End Property
        Dim _loai As String
        Property Loai() As String
            Get
                Return _loai
            End Get
            Set(ByVal value As String)
                _loai = value
            End Set
        End Property
        Dim _xoa As Boolean
        Property Xoa() As Boolean
            Get
                Return _xoa
            End Get
            Set(ByVal value As Boolean)
                _xoa = value
            End Set
        End Property
    End Structure

    Public Function LayDS() As List(Of LoaiThu)
        Dim ds As New List(Of LoaiThu)
        Try
            Dim con = DataProvider.MoKetNoi()
            con.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from LoaiThu where Xoa=No", con)
            Dim rd As OleDbDataReader = cmd.ExecuteReader()
            While rd.Read()
                Dim p As LoaiThu = New LoaiThu()
                p.ID = rd.GetInt32(0)
                p.Loai = rd.GetString(1)
                p.Xoa = rd.GetBoolean(2)
                ds.Add(p)
            End While
            con.Close()
            Return (ds)
        Catch ex As Exception
            Return ds
        End Try
    End Function

    Public Function Lay(ByVal id As Integer) As LoaiThu
        Try
            Dim con = DataProvider.MoKetNoi()
            con.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("Select * from LoaiThu where ID=@ID", con)
            cmd.Parameters.AddWithValue("@ID", id)
            Dim rd As OleDbDataReader = cmd.ExecuteReader()
            Dim p As LoaiThu = New LoaiThu()
            If rd.Read() Then
                p.ID = rd.GetInt32(0)
                p.Loai = rd.GetString(1)
                p.Xoa = rd.GetBoolean(2)
            End If
            con.Close()
            Return p
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Function Them(ByRef p As LoaiThu) As Boolean
        Try
            Dim con = DataProvider.MoKetNoi()
            con.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("insert into LoaiThu(Loai, Xoa) values(@Loai, No)", con)
            cmd.Parameters.AddWithValue("@Loai", p.Loai)
            Dim kq = cmd.ExecuteNonQuery()
            cmd.CommandText = "select @@IDENTITY"
            p.ID = cmd.ExecuteScalar()
            con.Close()
            Return kq
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function Xoa(ByVal p As LoaiThu) As Boolean
        Try
            Dim con = DataProvider.MoKetNoi()
            con.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("delete from LoaiThu where ID = @ID", con)
            cmd.Parameters.AddWithValue("@ID", p.ID)
            Dim kq = cmd.ExecuteNonQuery()
            con.Close()
            Return kq
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Function CapNhat(ByVal p As LoaiThu) As Boolean
        Try
            Dim con = DataProvider.MoKetNoi()
            con.Open()
            Dim cmd As OleDbCommand = New OleDbCommand("update LoaiThu set Loai=@Loai,Xoa=@Xoa where ID = @ID", con)
            cmd.Parameters.AddWithValue("@Loai", p.Loai)
            cmd.Parameters.AddWithValue("@Xoa", p.Xoa)
            cmd.Parameters.AddWithValue("@ID", p.ID)
            Dim kq = cmd.ExecuteNonQuery()
            con.Close()
            Return kq
        Catch ex As Exception
            Return False
        End Try
    End Function
End Module
